﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using RFIDStockManagement.BLL;

namespace RFIDStockManagement.Client.Admin
{
    public partial class ChangePwdForm : DevExpress.XtraEditors.XtraForm
    {
        private UserManager _manager;
        private int curUserId = 0;

        public ChangePwdForm(UserManager manager)
        {
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(ChangePwdForm_FormClosing);
            _manager = manager;
            if (_manager == null)
            {
                _manager = new UserManager();
            }
        }

        void ChangePwdForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                this.Hide();
            }
        }


        public void ChangeUserPwd(int id)
        {
            txtPwd1.Text = string.Empty;
            txtPwd2.Text = string.Empty;
            curUserId = id;
            this.ShowDialog();

        }

        private void bntOk_Click(object sender, EventArgs e)
        {
            string pwd1 = txtPwd1.Text.Trim().ToLower();
            string pwd2 = txtPwd2.Text.Trim().ToLower();

            if (pwd1 != pwd2)
            {
                AlertForm.ShowMessage("两次输入密码不一致,请重新输入", "密码错误");
                return;
            }

            if (!_manager.ChangePwd(curUserId, pwd1, out pwd2))
            {
                AlertForm.ShowMessage(pwd2, "操作失败");
                return;
            }

            this.Hide();
            AlertForm.ShowMessage("操作成功,新密码下次登录时生效", "操作成功");
        }

    }
}